package com.azzaou.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.azzaou.dao.IAssocieDao;
import com.azzaou.model.Chantier;
import com.azzaou.model.Associe;

public class AssocieDao extends HibernateDaoSupport implements IAssocieDao {

	@Override
	public List<Associe> getAssocieByCriteria(Associe associe) {
	 
		 
		 
		 List<Associe> associeList = new ArrayList<Associe>();
			
		 StringBuffer requete = new StringBuffer("FROM Associe WHERE 1=1 ");

		  if (associe != null) {
				if (!StringUtils.isBlank(associe.getEmail())) {
					requete.append(" AND email = :email ");
				}
				if (!StringUtils.isBlank(associe.getMobile())) {
					requete.append(" AND mobile = :mobile ");
				}
				
				
				if (!StringUtils.isBlank(associe.getNom())) {
					requete.append(" AND nom = :nom ");
				}
				
				if (!StringUtils.isBlank(associe.getPrenom())) {
					requete.append(" AND prenom = :prenom ");
				}
				if (!StringUtils.isBlank(associe.getTelephone())) {
					requete.append(" AND telephone = :telephone ");
				}
				
				
		  }
		  Query query = getSession().createQuery(requete.toString());
		  
		  if (associe != null) {
			  
			  
			  if (!StringUtils.isBlank(associe.getEmail())) {
				  query.setString("email", associe.getEmail());
					
				}
				if (!StringUtils.isBlank(associe.getMobile())) {
					query.setString("mobile", associe.getMobile());
						
				}
				
				
				if (!StringUtils.isBlank(associe.getNom())) {
					query.setString("nom", associe.getNom());
					
				}
				
				if (!StringUtils.isBlank(associe.getPrenom())) {
					query.setString("prenom", associe.getPrenom());
					
				}
				if (!StringUtils.isBlank(associe.getTelephone())) {
					query.setString("telephone", associe.getTelephone());
					
				}
			  
			  
			  
		  }
		  associeList = query.list();
		 return associeList;
		
	}

	@Override
	public void saveOrUpdate(Associe associe) {
		getHibernateTemplate().saveOrUpdate(associe); 

	}

	@Override
	public void remove(Associe associe) {
		getHibernateTemplate().delete(associe);

	}

	@Override
	public Associe getAssocieById(Long id) {
		Associe associe = new Associe();
	    Query query = getSession().createQuery(" FROM Associe where afficher = '1'");
	    associe = (Associe)query.uniqueResult();
	    return associe;
	}

	@Override
	public List<Associe> getAllActifAssocie() {
		List<Associe> associeList = new ArrayList<Associe>();
	    Query query = getSession().createQuery(" FROM Associe where afficher = '1'");
	    associeList = query.list();
	    return associeList;
	}

	@Override
	public List<Associe> getAllAssocie() {
		List<Associe> associeList = new ArrayList<Associe>();
	    Query query = getSession().createQuery(" FROM Associe");
	    associeList = query.list();
	    return associeList;
	}

}
